package com.zx.kitchen.index.common.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.adapter.DishListAdapter;
import com.zx.kitchen.index.common.entity.CateListBean;
import com.zx.kitchen.index.common.entity.DishesListBean;
import com.zx.kitchen.index.common.entity.IndexOrderBean;
import com.zx.kitchen.index.common.mvp.presenter.IndexOrderNextPresenter;
import com.zx.kitchen.index.common.mvp.view.IndexOrderNextView;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/5.
 */

public class DishListFragment extends Fragment implements IndexOrderNextView {
    private ListView listView;
    private List<DishesListBean> mList = new ArrayList<>();

    private DishListAdapter adapter;
    private Context context;
    private IndexOrderNextPresenter indexOrderNextPresenter;
    public int dishCheckedCount = 0;
    private TextView tvSelectAll;
    private String ids = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_catelist, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();
        indexOrderNextPresenter = new IndexOrderNextPresenter(this, context);
        tvSelectAll = (TextView) view.findViewById(R.id.tv_select_all);
        listView = (ListView) view.findViewById(R.id.catelist);
        adapter = new DishListAdapter(getContext(), mList);
        tvSelectAll.setTag("unCheck");


        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //数据加载成功之后
        adapter.setOnCheckStatusChangeListener(new DishListAdapter.OnCheckStatusChangeListener() {
            @Override
            public void onChange(boolean isCheck) {
                asyncTootBarText(isCheck ? dishCheckedCount + 1 : dishCheckedCount - 1);

            }
        });
        tvSelectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tvSelectAll.getTag().toString().equals("unCheck")) {
                    checkAll();
                    checkAllItem(true);
                    asyncTootBarText(adapter.getCount());
                } else {
                    unCheckAll();
                    checkAllItem(false);
                    asyncTootBarText(0);
                }
            }
        });

    }

    private void unCheckAll() {
        tvSelectAll.setTag("unCheck");
        tvSelectAll.setText(getResources().getString(R.string.selectAll));
    }

    private void checkAll() {
        tvSelectAll.setTag("checked");
        tvSelectAll.setText(getResources().getString(R.string.unSelectAll));
    }

    private void checkAllItem(boolean isCheck) {
        for (int i = 0; i < adapter.getCount(); i++) {
            adapter.getItem(i).setIsChecked(isCheck);
        }
        adapter.notifyDataSetChanged();
    }

    //当左边订单栏目被修改的时候右边数据相应修改
    public void setData(CateListBean cateListBean) {
        indexOrderNextPresenter.getFromNet(cateListBean.getFid(), cateListBean.getIs_last());
        adapter.notifyDataSetChanged();
    }


    public void asyncTootBarText(int dishCheckedCount) {
        this.dishCheckedCount = dishCheckedCount;


        for (int i = 0; i < adapter.getCount(); i++) {
            if (adapter.getItem(i).isChecked()) {
                ids = ids + adapter.getItem(i).getId() + ",";
            } else {
                ids = ids.replace(adapter.getItem(i).getId() + ",", "");
            }
        }
        if (onCountListener != null) {
            onCountListener.onCount(dishCheckedCount);
            onCountListener.getData(ids);
        }

    }


    public OnCountListener onCountListener;

    public interface OnCountListener {
        void onCount(int count);

        void getData(String ids);
    }

    public void setCountOnListener(OnCountListener onCountListener) {
        this.onCountListener = onCountListener;
    }


    @Override
    public void onSuccess(IndexOrderBean indexOrderBean) {
        mList.clear();
        mList.addAll(indexOrderBean.getDishes_list());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }
}
